package com.anjie.powerproject.controller;

import com.anjie.powerproject.entity.StationInfo;
import com.anjie.powerproject.service.StationInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 读取配置文件中配置的标题名称
 */
@RestController
@RequestMapping(value = "title")
@PropertySource(value = {"classpath:title.properties"}, encoding = "UTF-8", name = "title.properties")
public class TitleController {
    @Value("#{${project.admin.title}}")
    private Map<String, String> titleMap;

    @Autowired
    private StationInfoService stationInfoService;

    @RequestMapping(value = "get/{type}", method = RequestMethod.GET)
    public Map<String, Object> get(@PathVariable String type) {
        Map<String, Object> resultMap = new HashMap<>();
        List<StationInfo> stationInfos=stationInfoService.findAllInfo();
        StationInfo stationInfo=new StationInfo();
        if(stationInfos.size()>=1)
            stationInfo=stationInfos.get(0);
        else
            stationInfo=null;

//        String title = titleMap.get(type);
//        if (title != null && !title.equals("")) {
//            resultMap.put("title", title);
//        } else {
//            resultMap.put("error", "请检查配置文件或者TitleController");
//        }
        if (stationInfo != null) {
            resultMap.put("title", stationInfo.getStationName());
        } else {
            resultMap.put("error", "请检查配置文件或者TitleController");
        }

        return resultMap;
    }
}